package com.wuge.gulimall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.wuge.gulimall.product.entity.BrandEntity;
import com.wuge.gulimall.product.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wuge.common.utils.PageUtils;
import com.wuge.common.utils.Query;

import com.wuge.gulimall.product.dao.CategoryBrandRelationDao;
import com.wuge.gulimall.product.entity.CategoryBrandRelationEntity;
import com.wuge.gulimall.product.service.CategoryBrandRelationService;


@Service("categoryBrandRelationService")
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationDao, CategoryBrandRelationEntity> implements CategoryBrandRelationService {
   @Autowired
    BrandService brandService;
    @Override
    public List<BrandEntity> getByCateID(Long cateId) {
        List<CategoryBrandRelationEntity> catelog_id = this.list(new QueryWrapper<CategoryBrandRelationEntity>().eq("catelog_id", cateId));
        List<Long> collect = catelog_id.stream().map((entity) -> {
            return entity.getBrandId();
        }).collect(Collectors.toList());
        List<BrandEntity> brandEntities = brandService.listByIds(collect);
        return brandEntities;
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryBrandRelationEntity> page = this.page(
                new Query<CategoryBrandRelationEntity>().getPage(params),
                new QueryWrapper<CategoryBrandRelationEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<CategoryBrandRelationEntity> getByID(Long id) {
        QueryWrapper<CategoryBrandRelationEntity> brandId = new QueryWrapper<CategoryBrandRelationEntity>().eq("brand_id", id);

        return  this.list(brandId);
    }

    @Override
    public void updateBrandId(Long brand, String brandname) {
        this.update(new UpdateWrapper<CategoryBrandRelationEntity>().set("brand_name",brandname).eq("brand_id",brand));
    }

    @Override
    public void updatecategory(Long category, String categoryname) {
        this.update(new UpdateWrapper<CategoryBrandRelationEntity>().set("catelog_name",categoryname).eq("catelog_id",category));
    }
}